package com.zhongxi.guoyuan.view.coinShopping;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.GvSlidingAdapter;
import com.zhongxi.guoyuan.adapter.coinShop.TodayNewGoodsAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemData;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/7.
 */

public class CoinCategoryActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView>{

    private int position;
    private String type;
    private NoScrollGridView gvList;
    private PullToRefreshListView lvList;

    private List<ProjectItemData> list = new ArrayList<ProjectItemData>();
    private GvSlidingAdapter gvAdapter;
    private ProjectItemResult itemResult;
    private TodayNewGoodsAdapter adapter;
    private List<HotGoodsData> data = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_category);
        initTitle();
        tvTitle.setText("银币消费商家");
        position = getIntent().getIntExtra("position", 0);
        gvList = (NoScrollGridView) findViewById(R.id.gvList);
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new TodayNewGoodsAdapter(context, data);
        lvList.setAdapter(adapter);
        getCategory();
    }

    private void getCategory() {
        type = "category";
        getServer(Url.CATEGORY_LIST, null, "", false, null, "get");
    }

    //设置gridView的属性
    private void setGvParams(NoScrollGridView gv, List list, int item) {
        int spaceWith = CommonTool.dip2px(context, 10);//左边距 paddingLeft
        int with = getResources().getDisplayMetrics().widthPixels / item;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(list.size() * with, LinearLayout.LayoutParams.WRAP_CONTENT);
        gv.setLayoutParams(params);
        gv.setColumnWidth(with - spaceWith);
        gv.setHorizontalSpacing(spaceWith);
        gv.setStretchMode(GridView.NO_STRETCH);   //不拉伸
        gv.setOverScrollMode(0);
        gv.setNumColumns(list.size());      //一排显示几列
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("category".equals(type)){
                itemResult = gson.fromJson(response, ProjectItemResult.class);
                MyApplication.getInstance().setSpValue(itemResult.getSafeobj());
                if(itemResult.getData().size()>0){
                    list.addAll(itemResult.getData());
                    gvAdapter = new GvSlidingAdapter(context, list);
                    gvList.setAdapter(gvAdapter);
                    gvAdapter.setSelectIndex(position);
                    setGvParams(gvList, list, 4);
                    getGoods();
                }
            }else if("goods".equals(type)){
                HotGoodsResult result = gson.fromJson(response, HotGoodsResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum==1){
                    data.clear();
                }
                if(result.getData().size()>0){
                    data.addAll(result.getData());
                }
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 分类下的商品
     */
    private void getGoods() {
        type = "goods";
        String item = itemResult.getData().get(position).getId();
        String str = "&page="+pageNum+"&orderBy=0&cateid="+item ;
        getServer(Url.GOODS_LIST,null, str, false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, CoinGoodsActivity.class);
                intent.putExtra("goodsId", data.get(position-1).getId());
                startActivity(intent);
            }
        });
        gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long lon) {
                gvAdapter.setSelectIndex(pos);
                gvAdapter.notifyDataSetChanged();
                position = pos;
                pageNum = 1;
                getGoods();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getGoods();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum ++;
        getGoods();
    }
}
